
package com.jf.cloud.service.sysmanager.remote;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jf.cloud.service.sysmanager.common.base.AjaxPageData;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.db.Condition;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.common.util.qnImg.UplodImg;
import com.jf.cloud.service.sysmanager.constant.ImagePath;
import com.jf.cloud.service.sysmanager.dao.IPubgClubService;
import com.jf.cloud.service.sysmanager.dao.IPubgTeamService;
import com.jf.cloud.service.sysmanager.entity.PubgClub;
import com.jf.cloud.service.sysmanager.entity.PubgTeam;
import com.jf.cloud.service.sysmanager.entity.SysStaff;
import com.jf.cloud.service.sysmanager.handler.TokenHandler;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;

/**
 * 战队管理控制器
 * 
 * @author 代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/pubgTeam")
public class PubgTeamController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgTeamController.class);

	/**
	 * 战队服务
	 */
	@Resource
	private IPubgTeamService pubgTeamService;

	/**
	 * 俱乐部服务
	 */
	@Resource
	private IPubgClubService pubgClubService;

	@Resource
	private TokenHandler tokenHandler;
	
	/**
	 * 后台战队管理页面
	 *
	 * @author 高冬冬 2018年4月4日
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request) {
		try {
			List<PubgClub> clubList = pubgClubService.findList(new Query());
			request.setAttribute("clubList", clubList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "jsp/pubg/pubgTeam_list";
	}

	/**
	 * 后台战队管理列表
	 *
	 * @author 高冬冬 2018年4月4日
	 */
	@ResponseBody
	@RequestMapping("/listPubgTeam.json")
	public AjaxPageData<PubgTeam> listPubgTeam(HttpServletRequest request, OPaging<PubgTeam> page) {
		return getAjaxPageData(pubgTeamService.findPage(page));
	}

	/**
	 * 添加修改战队广告信息
	 * 
	 * @author 朱建文 创建时间：2018年5月2日 下午1:10:13
	 */
	@ResponseBody
	@RequestMapping("/editExternalLink")
	public Result<String> editExternalLink(PubgTeam pubgTeam, HttpServletRequest request, HttpServletResponse response){
		Result<String> result = new Result<String>();
		result.setCode(-1);
		result.setMessage("维护战队广告信息失败！");

		try {
			// 验证token
			SysStaff sysStaff = getLoginUser(request);
			if (sysStaff == null) {
				result.setCode(-2);
				result.setMessage("请重新登陆！");
			    return result;
			}
			
			String	path = UplodImg.up(request, ImagePath.T_TEAM_AD, "imgPath", response);
			if (!StringUtil.isBlank(path)) {
				if (path.equals("isNotImg")) {
					result.setMessage("上传文件只能为图片!");
					return result;
				} else {
					pubgTeam.setExternalLinkImg(path);
				}
			}
			pubgTeamService.updateExternalADInfo(pubgTeam);
			result.setCode(0);
			result.setMessage("维护战队广告信息成功！");
		} catch (Exception e) {
			log.error("维护战队广告信息出错！", e);
		}
		return result;
	}
}
